package com.cheyunkeji.er.activity;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.app.UserInfoManager;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;

public class JoinNewOrganizeActivity extends BaseActivity {

  private static final String TAG = JoinNewOrganizeActivity.class.getSimpleName();

  @BindView(R.id.v_topbar)
  TopBar vTopbar;

  @BindView(R.id.btn_submit)
  Button btnSubmit;

  @BindView(R.id.et_channel_id)
  EditText etChannelId;

  @Override
  protected void init() {
    setContentView(R.layout.er_activity_add_new_organize);
    ButterKnife.bind(this);
  }

  @Override
  protected void initView() {

    vTopbar.setTitle("加入新的组织");
    vTopbar.setLeftBack();
    btnSubmit.setOnClickListener(this);
  }

  @Override
  protected void initData() {}

  @Override
  public void onClick(View v) {
    super.onClick(v);
    if (v.getId() == R.id.btn_submit) {
      if (TextUtils.isEmpty(etChannelId.getText().toString())) {
        SToast.show("请输入通道ID");
        return;
      } else {
        requestJoinOrganization(etChannelId.getText().toString().trim());
      }
    }
  }

  private void requestJoinOrganization(String channelId) {

    HashMap<String, String> params = new HashMap<>();
    params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
    params.put("ickey", channelId);

    ApiClient.postForm(
        HttpConstants.JOIN_NEW_ORGANIZATION,
        params,
        new StringCallback() {
          @Override
          public void onError(Call call, Exception e, int id) {
            if (e instanceof UnknownHostException || e instanceof SocketException) {
              SToast.show(R.string.tip_network_error, Gravity.CENTER);
              return;
            }
          }

          @Override
          public void onResponse(String response, int id) {
            Log.e(TAG, "onResponse: " + response);
            try {
              JSONObject jsonObj = new JSONObject(response);
              if (TextUtils.equals(jsonObj.optString("code", "0"), "1")) {
                SToast.show("申请成功");
                new Handler()
                    .postDelayed(
                        new Runnable() {
                          @Override
                          public void run() {
                            MyApplication.getInstance().removeActivity(JoinNewOrganizeActivity.class);
                            finish();
                          }
                        },
                        1000);

              } else {
                SToast.show(jsonObj.optString("msg"));
              }
            } catch (JSONException e) {
              e.printStackTrace();
            }
          }

          @Override
          public void onBefore(Request request, int id) {
            showDialog();
          }

          @Override
          public void onAfter(int id) {
            dismissDialog();
          }
        });
  }
}
